import {Component, OnInit} from "@angular/core";
import {HttpServcie} from "../../service/http.service";
import { Config } from "../../service/config";
import {
  NzNotificationService,
  UploadFile,
  NzMessageService, NzModalService, NzModalSubject
} from "ng-zorro-antd";

import {CustomReport} from "./bean/customReport.bean";

@Component({
  selector: "dynamic-form",
  templateUrl: "dynamic-form.html",
  styleUrls: ["dynamic-form.scss"]
})
export class DynamicForm implements OnInit{
  // 报表Id
  public id:string;
  // 定义返回数据
  public dataValue:any=[];
  // 定义select下拉语句
  public datas:any;
  //多选框
  allChecked: any = false;
  //多选框
  indeterminate: any = false;
  modelTitle = "新增";
  modal: any = 0;
  currentModal: NzModalSubject;
  // 定义实体
  customReport:CustomReport = new CustomReport();
  public updateData: any = {};
  config: any = Config;
  //接收参数
  paramePip = Config.isAdjustmentList;

  constructor(
    private httpServcie: HttpServcie,
    private _notification: NzNotificationService,
    private modalService: NzModalService
  ) {
  }

  ngOnInit() {

    /**
     * 初始化报表类型
     */
    this.findReportData();
    /**
     * 初始化下拉列表
     */
    this.findListData();


  }

  /**
   * 查询报表数据
   */
  findReportData() {
    let reportBean: CustomReport = new CustomReport();
    if (this.id != null && this.id != undefined) {
      reportBean.id = this.id;
    }
    this.httpServcie.findListReport(reportBean).subscribe(data => {
      if (data.status == 200) {
        this.dataValue = data.data;
        console.log(data.data);
      }
    })
  }

  /**
   * 初始化下拉列表
   */
  findListData() {
    let reportBean: CustomReport = new CustomReport();
    this.httpServcie.findListReport(reportBean).subscribe(data => {
      if (data.status == 200) {
        this.datas = data.data;
        console.log(data.data);
      }
    })
  }

  //table加载数据时
  displayDataChange($event) {
    this.dataValue = $event;
    this.refreshStatus();
  }

  //重置选择框
  refreshStatus() {
    const allChecked = this.dataValue.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.dataValue.every(
      value => value.disabled || !value.checked
    );
    this.allChecked = allChecked;
    this.indeterminate = !allChecked && !allUnChecked;
  }

  //全选
  checkAll(value) {
    if (value) {
      this.dataValue.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.dataValue.forEach(data => (data.checked = false));
    }
  }



  /**
   * 删除按钮支持批量删除
   */
  delete() {
    const deleteList: any = [];
    const thiss:any = this;
    this.dataValue.forEach(element => {
      if (element.checked) {
        deleteList.push(element.id + "");
      }
    });

    if (deleteList.length == 0) {
      // 异常
      this._notification.create("warning", "警告提示", "请选中一条数据！");
      return;
    }

    this.modalService.confirm({
      title: "您是否确认要删除这 " + deleteList.length + " 条内容",
      content: "<b>一旦删除，无法还原</b>",
      onOk() {
        let ob = thiss.httpServcie.deleteReports({ids: deleteList}); //批量删除
        ob.subscribe(
          data => {
            if (data.status == 200) {
              thiss._notification.create("success", "成功", "删除成功！");
              thiss.findReportData();
            } else {
              thiss._notification.create("error", "错误提示", data.message);
            }
          },
          error => {
            thiss._notification.create("error", "错误提示", "删除失败！");
          }
        );
      },
      onCancel() {
      }
    });
  }

  //添加
  add(titleTpl, contentTpl, footerTpl) {
    this.modelTitle = "添加";
    this.modal = 0;

    this.currentModal = this.modalService.open({
      title: titleTpl,
      content: contentTpl,
      footer: footerTpl,
      maskClosable: false,
      width: 900,
      style: {
        top: "20px"
      },
      onOk() {
        console.log("Click ok");
      }
    });
  }
  //修改
  update(titleTpl, contentTpl, footerTpl) {
    this.modelTitle = "修改";
    this.modal = 1;
    let updateList:any=[];

    this.dataValue.forEach(element => {
      if (element.checked) {
        updateList.push(element);
        this.updateData = element;
      }
    });
    if (updateList.length > 1) {
      // 异常
      this._notification.create("warning", "警告提示", "只能选中一条数据！");
      return;
    }
    if (updateList.length == 0) {
      // 异常
      this._notification.create("warning", "警告提示", "请选中一条数据！");
      return;
    }

    this.currentModal = this.modalService.open({
      title: titleTpl,
      content: contentTpl,
      footer: footerTpl,
      maskClosable: false,
      width: 900,
      style: {
        top: "20px"
      },
      onOk() {
        console.log("Click ok");
      }
    });
  }

  //添加、修改后返回的结果,可以刷新界面
  addOutPut(event) {
    setTimeout(() => {
      /* destroy方法可以传入onOk或者onCancel。默认是onCancel */
      this.currentModal.destroy();
      this.currentModal = null;
    }, 500);
    // this.elevatorlBean.currentPage = 1;
    this.findReportData();
  }

}
